/**
 * @author whis admin@wwhis.com
 * @Created 2019-06-19
 */
import Axios from 'axios'
import Const from './const'
import Core from '../core'
import { Message } from 'iview'

const axios = Axios.create({
  baseURL: Const.NET.END_POINT_ADMIN + '/',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})

function Api () {
  function transformObjectToUrlencodedData (obj) {
    let p = []
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        p.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
      }
    }
    return p.join('&')
  }

  function req (method, url, json) {
    let token = Core.Data.getToken() || ''

    json = Object.assign(json, {
      token: token
    })

    method = method.toUpperCase()

    console.log(Const.NET.END_POINT_ADMIN + '/' + url + '?' + transformObjectToUrlencodedData(json))
    return new Promise((resolve, reject) => {
      let r = ''
      if (method === 'GET') {
        r = axios.get(`${url}?${transformObjectToUrlencodedData(json)}`)
      } else if (method === 'POST') {
        r = axios.post(url, transformObjectToUrlencodedData(json))
      } else if (method === 'DELETE') {
        r = axios.delete(`${url}?${transformObjectToUrlencodedData(json)}`)
      }

      if (r) {
        r.then(
          response => {
            let body = response.data

            if (body.hasOwnProperty('code') && body.code === 0) {
              return resolve(body.data)
            }

            if (body.hasOwnProperty('code') && body.code === Const.ERROR.ERROR_TOKEN_INVALID) {
              Core.Data.clearAuthData()
              // router.replace({path: '/login'})
              return
            }

            Message.error(body.message)
            return reject(body)
          },
          error => {
            Message.error(error.message)
            reject(error)
          }
        )
      }
    })
  }

  return {
    Admin: {
      login: (params) => { return req('POST', 'admin/login', params) },
      logout: (params) => { return req('POST', 'admin/logout', params) }
    },
    Game: {
      list: (params) => { return req('GET', 'game/list', params) },
      listWithTag: (params) => { return req('GET', 'game/list-with-tag', params) },
      save: (params) => { return req('POST', 'game/save', params) },
      delete: (params) => { return req('POST', 'game/delete', params) },
    },
    BlackAccount: {
      list: (params) => { return req('GET', 'black-account/list', params) },
      save: (params) => { return req('POST', 'black-account/save', params) },
      delete: (params) => { return req('POST', 'black-account/delete', params) },
    },
    CustomerService: {
      list: (params) => { return req('GET', 'customer-service/list', params) },
      save: (params) => { return req('POST', 'customer-service/save', params) },
      delete: (params) => { return req('POST', 'customer-service/delete', params) },
    },
    Ad: {
      list: (params) => { return req('GET', 'ad/list', params) },
      save: (params) => { return req('POST', 'ad/save', params) },
      delete: (params) => { return req('POST', 'ad/delete', params) },
    },
    Article: {
      list: (params) => { return req('GET', 'article/list', params) },
      save: (params) => { return req('POST', 'article/save', params) },
      delete: (params) => { return req('POST', 'article/delete', params) },
    },
    GameTag: {
      list: (params) => { return req('GET', 'game-tag/list', params) },
      listWithTag: (params) => { return req('GET', 'game-tag/list-with-tag', params) },
      save: (params) => { return req('POST', 'game-tag/save', params) },
      delete: (params) => { return req('POST', 'game-tag/delete', params) },
    },
    GameTagInfo: {
      list: (params) => { return req('GET', 'game-tag-info/list', params) },
      save: (params) => { return req('POST', 'game-tag-info/save', params) },
      delete: (params) => { return req('POST', 'game-tag-info/delete', params) },
    },
    GameAccount: {
      list: (params) => { return req('GET', 'game-account/list', params) },
      save: (params) => { return req('POST', 'game-account/save', params) },
      delete: (params) => { return req('POST', 'game-account/delete', params) },
      sold: (params) => { return req('POST', 'game-account/sold', params) },
    },
    req: req
  }
}

export default new Api()
